package com.taysear.parcel;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import com.taysear.parcel.domain.Bus;
import com.taysear.parcel.domain.Employee;
import com.taysear.parcel.domain.Loadingsheet;
import com.taysear.parcel.domain.LoadingsheetStatus;
import com.taysear.parcel.domain.Parcel;
import com.taysear.parcel.domain.Permission;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.domain.Substation;
import com.vaadin.data.Container;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.And;
import com.vaadin.event.dd.acceptcriteria.ClientSideCriterion;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.AbstractSelect.AcceptItem;
import com.vaadin.ui.AbstractSelect.MultiSelectMode;
import com.vaadin.ui.Table.TableDragMode;
import com.vaadin.ui.themes.Runo;

@SuppressWarnings("serial")
public class RouteDetailTable extends Table {

	public RouteDetailTable(String caption, Container containers, String [] columns) {
		super(caption, containers);
		
		addGeneratedColumn("Substation", new ColumnGenerator() {
		  public Component generateCell(Table source, Object itemId,
		            Object columnId) {
		      if (getItem(itemId).getItemProperty("name").getValue() != null) {
		          Label l = new Label();
		          l.setValue(getItem(itemId).getItemProperty("name").getValue());
		          l.setSizeUndefined();
		          return l;
		      }
		      return null;
		  }
		});
		
		addStyleName(Runo.TABLE_SMALL);
		setVisibleColumns(columns);
		setSizeUndefined();
		setSelectable(true);
		setMultiSelect(true);
		setMultiSelectMode(MultiSelectMode.SIMPLE);
		setImmediate(true);
	}
	
	public void initializeTable(final ClientSideCriterion acceptCriterion, final BeanItemContainer<Substation> tableContainer) {

		// Handle drop in table: item or subtree to the table
        setDragMode(TableDragMode.MULTIROW);
        setSizeFull();
        setDropHandler(new DropHandler() {
            public void drop(DragAndDropEvent dropEvent) {
            	// criteria verify that this is safe
                DataBoundTransferable dataBound = (DataBoundTransferable) dropEvent.getTransferable();
                Container sourceContainer = dataBound.getSourceContainer();
                RouteDetailTable des = (RouteDetailTable) dropEvent.getTargetDetails().getTarget();
                RouteDetailTable source = (RouteDetailTable) dataBound.getSourceComponent();
                Set<Substation> values =  (Set<Substation>) source.getValue();
                for (Substation permissions : values) {
	                tableContainer.addItem(permissions);
	                sourceContainer.removeItem(permissions);
                }
            }

            public AcceptCriterion getAcceptCriterion() {
                return new And(acceptCriterion, AcceptItem.ALL);
            }
        });
    }
}